package game;

import java.awt.*;

abstract class ChineseChessBasedGame extends Game{
	abstract boolean HasChess(Point location);
	abstract int GetOwner(Point location) ;
	abstract public Color getPlayerColor(int owner);
	abstract Class<? extends ChineseChess> getChessType(Point location);
}

abstract class ChineseChess {
	private final Color chessBackGroundColor = Color.white;
	private ChineseChessBasedGame game;
	protected final int owner;

	protected ChineseChess(int owner, ChineseChessBasedGame game) {
		this.owner = owner;
		this.game = game;
	}

	public int getOwner() {
		return owner;
	}

	public abstract String getName();

	public void Paint(Graphics g, Point size) {
		g.setColor(chessBackGroundColor);
		g.fillOval(1, 1, size.x - 2, size.y - 2);
		g.setColor(game.getPlayerColor(owner));
		g.drawOval(0, 0, size.x, size.y);
		Point textLocation = new Point((int) (size.getX() / 4),
				(int) (size.getY() / 4));
		g.drawString(getName(), (size.x-10)/2, (size.y+5)/2);
	}

	protected boolean isPlaceableTarget(Point target) {
		if (!game.InRange(target))
			return false;
		if (!game.HasChess(target))
			return true;
		if (game.GetOwner(target) != owner)
			return true;
		return false;
	}

	protected Point nextStep(Point start, Point direction) {
		return new Point(start.x + direction.x, start.y + direction.y);
	}

	protected Point[] orthogonalVectors(Point direction) {
		return new Point[] { new Point(direction.y * -1, direction.x),
				new Point(direction.y, direction.x * -1) };
	}

	protected boolean isHorOrVerUnitVector(Point direction) {
		return (direction.x == 0 || direction.y == 0)
				&& (int) direction.distance(0, 0) == 1;
	}
	protected ChineseChessBasedGame getGame()
	{
		return game;
	}
	void dispose()
	{
		game = null;
	}

}

class ChariotChess extends ChineseChess {
	protected ChariotChess(int owner, ChineseChessBasedGame game) {
		super(owner, game);
	}

	@Override
	public String getName() {
		if (owner == 0)
			return "車";
		else
			return "俥";
	}
}

class HorseChess extends ChineseChess {
	protected HorseChess(int owner, ChineseChessBasedGame game) {
		super(owner, game);
	}

	@Override
	public String getName() {
		if (owner == 0)
			return "馬";
		else
			return "傌";
	}

}

class ElephantChess extends ChineseChess {
	protected ElephantChess(int owner, ChineseChessBasedGame game) {
		super(owner, game);
	}

	@Override
	public String getName() {
		if (owner == 0)
			return "象";
		else
			return "相";
	}

}

class AdvisorChess extends ChineseChess {
	protected AdvisorChess(int owner, ChineseChessBasedGame game) {
		super(owner, game);
	}

	@Override
	public String getName() {
		if (owner == 0)
			return "士";
		else
			return "仕";
	}

}

class GeneralChess extends ChineseChess {
	protected GeneralChess(int owner, ChineseChessBasedGame game) {
		super(owner, game);
	}

	@Override
	public String getName() {
		if (owner == 0)
			return "將";
		else
			return "帥";
	}
}

class CannonChess extends ChineseChess {
	protected CannonChess(int owner, ChineseChessBasedGame game) {
		super(owner, game);
	}

	@Override
	public String getName() {
		if (owner == 0)
			return "砲";
		else
			return "炮";
	}
}

class SoldierChess extends ChineseChess {
	protected SoldierChess(int owner, ChineseChessBasedGame game) {
		super(owner, game);
	}

	@Override
	public String getName() {
		if (owner == 0)
			return "兵";
		else
			return "卒";
	}
}

